package com.danger.Activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.danger.Model.bean.UserInfo;
import com.danger.Presenter.BindMobilePresenter;
import com.danger.Presenter.BindMobilePresenterImpl;
import com.danger.View.BindMobileView;

public class BindMobileActivity extends AppCompatActivity implements BindMobileView, View.OnClickListener {

    private EditText bind_phone_editText;
    private EditText bind_check_number_editText;
    private Button send_check_number_button;
    private Button finish_register_button;
    private Button cancel_register_button;
    private Toolbar toolbar;
    private ProgressDialog progressDialog;
    private Intent intent;
    private BindMobilePresenter bindMobilePresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_mobile);
        init();
    }

    private void init() {
        toolbar = (Toolbar) findViewById(R.id.bind_mobile_toolbar);
        toolbar.setTitle("绑定电话");
        toolbar.setTitleTextColor(Color.WHITE);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        intent = getIntent();
        bindMobilePresenter = new BindMobilePresenterImpl(getApplicationContext(), this);
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("正在注册...");
        bind_phone_editText = (EditText) findViewById(R.id.bind_phone_editText);
        bind_check_number_editText = (EditText) findViewById(R.id.bind_check_number_editText);
        send_check_number_button = (Button) findViewById(R.id.send_check_number_button);
        finish_register_button = (Button) findViewById(R.id.finish_register_button);
        cancel_register_button = (Button) findViewById(R.id.cancel_register_button);
        send_check_number_button.setOnClickListener(this);
        finish_register_button.setOnClickListener(this);
        cancel_register_button.setOnClickListener(this);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void showDialog() {
        progressDialog.show();
    }

    @Override
    public void hideDialog() {
        progressDialog.hide();
        progressDialog.dismiss();
    }

    @Override
    public void showMessage(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showError(String error) {
        Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
    }

    @Override
    public Button getSendButton() {
        return send_check_number_button;
    }

    @Override
    public UserInfo getUserInfo() {
        UserInfo userInfo = new UserInfo();
        if (intent != null) {
            userInfo.setUsername(intent.getStringExtra("username"));
            userInfo.setPassword(intent.getStringExtra("password"));
            userInfo.setQq(intent.getStringExtra("qq"));
            userInfo.setPhoto(intent.getStringExtra("photo"));
            userInfo.setEmail(intent.getStringExtra("email"));
        }
        return userInfo;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.send_check_number_button:
                bindMobilePresenter.sendCode(bind_phone_editText.getText().toString());
                break;
            case R.id.finish_register_button:
                bindMobilePresenter.register(bind_phone_editText.getText().toString(), bind_check_number_editText.getText().toString());
                break;
            case R.id.cancel_register_button:
                startActivity(new Intent(this, LoginActivity.class));
            default:
                break;
        }
    }

    @Override
    public void toLogin() {
        startActivity(new Intent(this, LoginActivity.class));
    }

    @Override
    public void startTimer() {

    }

    @Override
    public void cancelTimer() {

    }
}
